package com.molyfun.parents.fragment.dialog;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.utils.Const;

public class CourseContentDialogFragment extends DialogFragment implements View.OnClickListener{

    private static final String COURSE_TITLE_DESC = "标题: ";
    private static final String COURSE_TARGET_DESC = "目标: ";
    private static final String COURSE_CONTENT_DESC = "内容: ";

    private static CourseContentDialogFragment newInstance(String title,String target, String content) {
        CourseContentDialogFragment mStudentMenuDialogFragment = new CourseContentDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(Const.FLAG_TITLE, title);
        bundle.putSerializable(Const.FLAG_TARGET, target);
        bundle.putSerializable(Const.FLAG_CONTENT, content);
        mStudentMenuDialogFragment.setArguments(bundle);
        return mStudentMenuDialogFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setStyle(DialogFragment.STYLE_NORMAL, R.style.BaseDialog);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.dialog_course_content, container, false);
        String title = (String)getArguments().get(Const.FLAG_TITLE);
        String target = (String)getArguments().get(Const.FLAG_TARGET);
        String content = (String)getArguments().get(Const.FLAG_CONTENT);

//        ((TextView)view.findViewById(R.id.course_content_dialog_title)).setText(getSpannableStringBuilder(COURSE_TITLE_DESC, title));
        ((TextView)view.findViewById(R.id.course_content_dialog_target)).setText(getSpannableStringBuilder(COURSE_TARGET_DESC, target));
        ((TextView)view.findViewById(R.id.course_content_dialog_content)).setText(getSpannableStringBuilder(COURSE_CONTENT_DESC, content));
        view.findViewById(R.id.close).setOnClickListener(this);
        return view;
    }

    private SpannableStringBuilder getSpannableStringBuilder(String desc, String content){
        if(content == null){
            content = "";
        }
        SpannableStringBuilder builder = new SpannableStringBuilder(desc + content);
        ForegroundColorSpan descSpan = new ForegroundColorSpan(ContextCompat.getColor(getContext(), R.color.color_text_setting_desc));
        ForegroundColorSpan contentSpan = new ForegroundColorSpan(ContextCompat.getColor(getContext(), R.color.color_cell_text_title));
        builder.setSpan(descSpan, 0, desc.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        builder.setSpan(contentSpan, desc.length(), desc.length() + content.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return builder;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.close:
                break;
            default:
                break;
        }
        getDialog().dismiss();
    }

    public static class CourseContentDialogBuilder {

        CourseContentDialogFragment mCourseContentDialogFragment;
        private String mTag;

        public CourseContentDialogBuilder(String title, String target, String content){
            mCourseContentDialogFragment = CourseContentDialogFragment.newInstance(title, target, content);
            mTag = "course_content";
        }

        public CourseContentDialogBuilder setTag(String tag){
            this.mTag = tag;
            return this;
        }

        public void build(FragmentTransaction fragmentTransaction){
//            mCourseContentDialogFragment.show(fragmentTransaction, mTag);
            fragmentTransaction.add(mCourseContentDialogFragment, mTag);
            fragmentTransaction.commitAllowingStateLoss();
        }

    }
}
